package com.qingke.pms.command.user;

import com.qingke.pms.command.UserCommand;
import com.qingke.pms.pojo.User;
import com.qingke.pms.pojo.UserLogin;
import com.qingke.pms.util.PMSConsole;

public class AlterUserCommand extends UserCommand {

	public AlterUserCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		if(dao.isAdminstrator(user))
			isAdmin();
		else
			notAdmin(user);
	}
	
	private void isAdmin(){
		int id=PMSConsole.getInt("alter user id>");
		if(!dao.isUserExistById(id)){
			PMSConsole.println("the user is not existed");
			return ;
		}
		User alterUser=dao.selectUserById(id);
		if(id!=user.getId()&&dao.isAdminstrator(alterUser)){
			PMSConsole.println("the user is a adminstrator , you have no permission to alter it.");
			return ;
		}
		UserLogin userLogin=dao.selectUserLoginById(id);
		while(true){
			PMSConsole.println("alter (1.user login 2.user) 3.confirm alter");
			String str=PMSConsole.promptNotEmpty("alter>");
			if("1".equalsIgnoreCase(str))
				alterUserLogin(userLogin);
			else if("2".equalsIgnoreCase(str))
				notAdmin(alterUser);
			else if("3".equalsIgnoreCase(str))
				break;
			else 
				PMSConsole.println("err input");
			break;
		}
	}
	
	private void notAdmin(User user){
		while(true){
			PMSConsole.println("alter (1.name 2.email 3.phone) 4.confirm alter ");
			String str=PMSConsole.promptNotEmpty("alter user>");
			if("1".equalsIgnoreCase(str))
				name(user);
			else if("2".equalsIgnoreCase(str))
				email(user);
			else if("3".equalsIgnoreCase(str))
				phone(user);
			else if("4".equalsIgnoreCase(str))
				break;
			else 
				PMSConsole.println("err input");
		}
		
		boolean isSuccess=dao.updateUser(user);
		if(isSuccess)
			PMSConsole.println("update user success.");
		else
			PMSConsole.println("update user failed.");
	}
	
	private void alterUserLogin(UserLogin userLogin){
		while(true){
			PMSConsole.println("alter (1.username 2.password 3.is adminstrator) 4.confirm alter ");
			String str=PMSConsole.promptNotEmpty("alter user>");
			if("1".equalsIgnoreCase(str))
				username(userLogin);
			else if("2".equalsIgnoreCase(str))
				password(userLogin);
			else if("3".equalsIgnoreCase(str))
				isAdminstrator(userLogin);
			else if("4".equalsIgnoreCase(str))
				break;
			else 
				PMSConsole.println("err input");
		}
		
		boolean isSuccess=dao.updateUserLogin(userLogin);
		if(isSuccess)
			PMSConsole.println("update user login success.");
		else
			PMSConsole.println("update user login failed.");
	}

	private void name(User user){
		String name=PMSConsole.promptNotEmpty("user name:");
		user.setName(name);
	}

	private void email(User user){
		String email=PMSConsole.promptNotEmpty("user email:");
		user.setEmail(email);;
	}

	private void phone(User user){
		String phone=PMSConsole.promptNotEmpty("user phone:");
		user.setPhone(phone);;
	}
	
	private void username(UserLogin userLogin){
		String username=PMSConsole.promptNotEmpty("user username:");
		userLogin.setUsername(username);
	}
	
	private void password(UserLogin userLogin){
		String password=PMSConsole.promptNotEmpty("user password:");
		userLogin.setPassword(password);
	}
	
	private void isAdminstrator(UserLogin userLogin){
		boolean is=false;
		while(true){
			String str=PMSConsole.promptNotEmpty("user is adminstrator(y,n):");
			if("y".equalsIgnoreCase(str))
				is=true;
			else if("n".equalsIgnoreCase(str))
				is=false;
			else{
				PMSConsole.println("err input.");
				continue;
			}
			break;
		}
		
		userLogin.setIsAdministrator(is);
	}
	
	
}
